#!/usr/bin/env python
# coding=utf-8
# 
import MySQLdb
import ConfigParser
import json
import requests
import hashlib
import traceback
import time
import redis
from utils.syslog import Logger
from utils.EmailUtil import send_mail

config = ConfigParser.ConfigParser()
config.read("./conf/config.ini")
print config.get("mysqldb", "host")
logger = Logger(logger="log").getlog()

redis_poll = redis.ConnectionPool(host=config.get("redis", "REDIS_SERVER"),port=config.get("redis", "REDIS_PORT"), db=config.get("redis", "REDIS_DB"))
redis_con = redis.Redis(connection_pool = redis_poll)

global conn
def  getConn():
	conn= MySQLdb.connect(
	        host=config.get("mysqldb", "host"),
	        port = int(config.get("mysqldb", "port")),
	        user=config.get("mysqldb", "user"),
	        passwd=config.get("mysqldb", "passwd"),
	        db =config.get("mysqldb", "db"),
	        charset="utf8"
	        )
	return conn


def saveSource(passwd, bonus, count):
	
	try:
		while count>0:
			sql = "insert into nh_passwd_source(passwd, open_id, bonus) \
				values('"+passwd+"','no',"+str(bonus)+")"
			print sql
			cur.execute(sql)
			count = count -1
		conn.commit()
	except Exception:
		logger.error("保存红包源失败，异常信息："+traceback.format_exc())

if __name__ == '__main__':
	logger.info('********程序启动********')
	
	
	try:
		conn = getConn()
		cur = conn.cursor()
		print '连接数据库'
		
		saveSource('红包测试', 0.3, 100)
		
		conn.close()
		
	except Exception:
		print '==='
		logger.error('save error'+traceback.format_exc())
		send_mail('保存红包源程序运行出错，请尽快处理！'+traceback.format_exc())

